﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common.QueryBuilder;

namespace System.Data.SqlClient.QueryBuilder
{
    public class SqlQuerySelect : DbQuerySelect
    {
        private int _Top = -1;

        public SqlQuerySelect Top(int value)
        {
            this._Top = value;
            return this;
        }

        public SqlQuerySelect()
            : base()
        {
        }

        public SqlQuerySelect(String Table)
            : base(Table)
        {
        }

        public override string ToString()
        {
            String envNewLine = String.Concat(Environment.NewLine, "\t");
            String joins = this.Joins.Count > 0 ? String.Concat(String.Join(Environment.NewLine, this.Joins), Environment.NewLine) : String.Empty;
            String top = (this._Top >= 0 ? String.Concat(" TOP (", this._Top, ")") : String.Empty);
            return String.Concat(
                "SELECT", top, String.Concat(Environment.NewLine, "\t "),
                    String.Join(String.Concat(Environment.NewLine, "\t,"), this.Columns), Environment.NewLine,
                "FROM ", this.TableName, Environment.NewLine,
                joins,
                this.GroupBy, Environment.NewLine,
                this.OrderBy);
        }
    }
}
